package com.eascs.web.p2p.member.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.InvestorEntity;
import com.eascs.p2p.common.security.MD5;
import com.eascs.p2p.common.service.InvestorService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.p2p.common.controller.BaseMasterViewController;
import com.eascs.web.p2p.member.form.RegisterForm;
import com.eascs.web.p2p.member.untils.CusAccessObjectUtil;

@Controller
public class LoginController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Reference
	InvestorService investorService;

	@RequestMapping(value = "/login/index")
	public String index() {
		return "/member/login";
	}
	
	@RequestMapping(value = "/register/index")
	public String regIndex() {
		return "/member/register";
	}
	
	@RequestMapping(value = "/ftl/forgetPassword")
	public String forgetPassword(HttpServletRequest request) {
		return "/member/forgetPassword";
	}

	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request, HttpServletResponse response, String account, String password) {
		if (account.isEmpty() || password.isEmpty()) {
			return "/member/login";
		}
		String pw=MD5.md5Encode(password.trim());
		ModelResult<InvestorEntity> modelResult = investorService.queryInvestorByLogin(account, pw);
		if (modelResult.isSuccess()) {
			InvestorEntity investor = modelResult.getModel();
			if (investor != null) {
				String ip=CusAccessObjectUtil.getIpAddress(request);
				String mac=CusAccessObjectUtil.getMACAddress(ip);
				investor.setLastLoginIp(ip);
				investor.setLastLoginMac(mac);
				investor.setLastLoginTime(new Date());
				investorService.updateInvestor(investor);
				HttpSession session = request.getSession();
				session.removeAttribute("member");
				session.setAttribute("member", investor);
				return "redirect:/investor/index";
			}
		}
		ViewContext.put("errorMsg", "账号或密码不正确");
		return "/member/login";
	}
	
	/*@RequestMapping(value = "/register")
	public String register(HttpServletRequest request, RegisterForm registerForm) {
		InvestorEntity investor=new InvestorEntity();
		try {
			BeanUtils.copyProperties(investor, registerForm);
		} catch (Exception e) {}
		String regIp=CusAccessObjectUtil.getIpAddress(request);
		String mac=CusAccessObjectUtil.getMACAddress(regIp);
		investor.setRegIp(regIp);
		investor.setLastLoginIp(regIp);
		investor.setLastLoginMac(mac);
		investor.setChannel(InvestorEntity.REGISTER_CHANNEL_H5);
		investor.setAccount(investor.getMobile());
		ModelResult<Boolean> modelResult = investorService.register(investor);
		if (modelResult.isSuccess() && modelResult.getModel()) {
			String pw=MD5.md5Encode(investor.getPassword().trim());
			ModelResult<InvestorEntity> mr = investorService.queryInvestorByLogin(investor.getAccount(), pw);
			if (mr.isSuccess()) {
				investor = mr.getModel();
				if (investor != null) {
					HttpSession session = request.getSession();
					session.removeAttribute("member");
					session.setAttribute("member", investor);
					return "redirect:/investor/index";
				}
			}
			return "redirect:/investor/index";
		}
		return "/member/register";
	}*/
}
